package io.lemur.common.util.file;

import io.lemur.common.util.logger.MyLogger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.slf4j.Logger;

/**
 * 文件操作工具类
 * @author JueYue
 * @date 2015年1月28日 下午11:39:24
 */
public final class FileUtil {

    private final static Logger LOGGER = MyLogger.getLogger();

    private FileUtil() {
    }

    public static String readToString(String fileName, String encode) {
        FileInputStream is = null;
        try {
            File file = new File(fileName);
            if (file.exists()) {
                is = new FileInputStream(file);
                StringBuilder sb = new StringBuilder();
                byte[] bytes = new byte[1024];
                while ((is.read(bytes)) != -1) {
                    sb.append(new String(bytes, encode));
                }
                return sb.toString();
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        throw new RuntimeException("读取异常");
    }
}
